package com.walle.activityservice.controller;

import com.walle.activityservice.dto.BusinessDefineDto;
import com.walle.activityservice.model.BusinessDefine;
import com.walle.activityservice.service.BusinessDefineService;
import com.walle.common.page.PageBean;
import com.walle.common.page.PageParam;
import com.walle.common.result.ResultResponse;
import com.walle.common.result.ResultResponseLu;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @author zhangjiapeng
 * @Package com.hdai.dps.activiti.controller
 * @Description: ${todo}
 * @date 2018/12/19 11:18
 */
@RestController
@RequestMapping("/businessDefine")
@Slf4j
public class BusinessDefineController {


    @Autowired
    private BusinessDefineService businessDefineService;



    @PostMapping("/insert")
    public ResultResponse insert(@RequestBody BusinessDefine businessDefine){
        try {
            businessDefineService.insert(businessDefine);
            return ResultResponse.ofSuccess();
        } catch (Exception e) {
            log.error("新增业务定义失败",e.getMessage(),e);
            return  ResultResponse.ofError(e.getMessage());
        }
    }


    @PostMapping(value = "/query")
    public ResultResponseLu query(@RequestBody BusinessDefineDto businessDefineDto){

        Integer page = businessDefineDto.getPage();
        Integer limit = businessDefineDto.getLimit();
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;

        BusinessDefine businessDefine = new BusinessDefine();
        BeanUtils.copyProperties(businessDefineDto, businessDefine);
        PageParam pageParam = new PageParam((page-1)*limit, limit);
        PageBean<BusinessDefine> pageBean = businessDefineService.queryPage(pageParam, businessDefine, businessDefineDto.getSortColumnName(), businessDefineDto.getSortType());
        List<BusinessDefine> infoList = pageBean.getRecordList();
        return ResultResponseLu.ofSuccess(infoList, pageBean.getTotalCount());
    }

    @PostMapping("/update")
    public ResultResponse update(@RequestBody BusinessDefine businessDefine){
        try {
            businessDefineService.update(businessDefine);
            return ResultResponse.ofSuccess();
        } catch (Exception e) {
            log.error("修改业务定义失败",e.getMessage(),e);
            return  ResultResponse.ofError(e.getMessage());
        }
    }

    @PostMapping("/get")
    public ResultResponse get(Long businessId){
        try {
            BusinessDefine businessDefine = businessDefineService.getById(businessId);
            return ResultResponse.ofSuccess(businessDefine);
        } catch (Exception e) {
            log.error("获取业务定义详情失败",e.getMessage(),e);
            return  ResultResponse.ofError(e.getMessage());
        }
    }


}
